//
//  ViewController.m
//  FileReader-PDF
//
//  Created by 李阳 on 15-7-1.
//  Copyright (c) 2015年 liyang. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (){
    PdfView *pdfView;
    
    int aa;
    int bb;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    aa = 1;
    bb = 1;
    [self addpdfview:aa];
    //    [self addpdfwebview];
    
    // Do any additional setup after loading the view, typically from a nib.
    
    UISwipeGestureRecognizer *recognizer;
    
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [[self view] addGestureRecognizer:recognizer];
    //    [recognizer release];
    
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [[self view] addGestureRecognizer:recognizer];
    //    [recognizer release];
    
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [[self view] addGestureRecognizer:recognizer];
    
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [[self view] addGestureRecognizer:recognizer];
}

//添加uiview的PDF(需要自己设置翻页)
-(void)addpdfview :(int)iii{
    CGRect frame = CGRectMake(0, 0, 320, 480);
    pdfView = [[PdfView alloc] initWithFrame:frame];
    pdfView.backgroundColor=[UIColor whiteColor];
    pdfView.pageindex = iii;
    [self.view addSubview:pdfView];
}
//添加uiwebview的PDF(不需要自己设置翻页)
-(void)addpdfwebview{
    CGRect frame1 = CGRectMake(0, 0, 320, 480);
    UIWebView *_webview = [[UIWebView alloc] initWithFrame:frame1];
    [self.view addSubview:_webview];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"oracle SQL必备参考" ofType:@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webview loadRequest:request];
    
}

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    
    if(recognizer.direction==UISwipeGestureRecognizerDirectionDown) {
        
        NSLog(@"swipe down");
        //执行程序
        if (aa != 1) {
            [pdfView removeFromSuperview];
            bb = aa -1;
            [self addpdfview:bb];
            aa = bb;
        }
    }
    if(recognizer.direction==UISwipeGestureRecognizerDirectionUp) {
        
        NSLog(@"swipe up");
        //执行程序
        [pdfView removeFromSuperview];
        bb = aa + 1;
        [self addpdfview:bb];
        aa = bb;
    }
    
    if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {
        
        NSLog(@"swipe left");
        //执行程序
        [pdfView removeFromSuperview];
        bb = aa + 1;
        [self addpdfview:bb];
        aa = bb;
        
    }
    
    if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"swipe right");
        //执行程序
        if (aa != 1) {
            [pdfView removeFromSuperview];
            bb = aa -1;
            [self addpdfview:bb];
            aa = bb;
        }
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
